﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Common.Forms
{
    using Common.Controls;

    public partial class formDefaultSettings : Form
    {
        protected List<SettingsControl> formsSettings = new List<SettingsControl>();
        protected List<SettingsManager> managersSettings = new List<SettingsManager>();

        public formDefaultSettings()
        {
            InitializeComponent();
        }

        public void AddSettings(SettingsManager sm, SettingsControl sb)
        {
            formsSettings.Add(sb);
            managersSettings.Add(sm);
        }

        void formDefaultSettings_Load(object sender, System.EventArgs e)
        {
            int maxWidth = btnCancel.Right - 8;
            int totalHeight = this.Height - btnCancel.Top + 8;

            foreach (var control in formsSettings)
            {
                maxWidth = Math.Max(Math.Max(maxWidth, control.MinimumSize.Width), control.Size.Width);
                totalHeight += control.Size.Height + 6;
            }

            this.Size = new Size(this.Size.Width + maxWidth + 8 - btnCancel.Right, totalHeight);
            int nextHeight = 8;

            for(int i = 0; i < formsSettings.Count; ++i)
            {
                formsSettings[i].Size = new Size(maxWidth, formsSettings[i].Size.Height);
                formsSettings[i].Location = new Point(8, nextHeight);

                nextHeight += formsSettings[i].Size.Height + 6;

                this.Controls.Add(formsSettings[i]);
            }

            for (int i = 0; i < managersSettings.Count; ++i)
            {
                foreach (var key in managersSettings[i].Keys)
                {
                    formsSettings[i][key] = managersSettings[i][key];
                }
            }
        }

        public void UpdateSettingsFromDialog()
        {
            for (int i = 0; i < managersSettings.Count; ++i)
            {
                foreach (var key in managersSettings[i].Keys)
                {
                    managersSettings[i][key] = formsSettings[i][key];
                }
            }
        }

        public String Options
        {
            get
            {
                UpdateSettingsFromDialog();

                String result = string.Empty;

                foreach (var settings in managersSettings)
                {
                    result += settings.ToString();
                }

                return result;
            }
        }

        protected virtual void btnAccept_Click(object sender, EventArgs e)
        {
            UpdateSettingsFromDialog();

            for (int i = 0; i < managersSettings.Count; ++i)
            {
                if (!managersSettings[i].ValidateParameter(formsSettings[i]))
                {
                    DialogResult = System.Windows.Forms.DialogResult.None;
                }
            }
        }
    }
}
